package com.helloUIBinder.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.datepicker.client.DateBox;
import com.helloUIBinder.client.events.OnAdminBlogPostCreateResultEvent;
import com.helloUIBinder.client.events.OnAdminBlogPostEditResultEvent;
import com.helloUIBinder.model.BlogPost;

public class AdminBlogPostCreateWidget extends Composite {
    private static AdminBlogPostCreateWidgetUiBinder uiBinder = GWT.create(AdminBlogPostCreateWidgetUiBinder.class);

    interface AdminBlogPostCreateWidgetUiBinder extends UiBinder<Widget, AdminBlogPostCreateWidget> {
    }

    @UiField
    TextBox ownerTextBox;

    @UiField
    TextBox themeTextBox;

    @UiField
    DateBox dateBox;

    @UiField
    TextArea textArea;

    @UiField
    TextBox categoryTextBox;

    @UiField
    Button submitButton;

    @UiField
    Button cancelButton;

    private SimpleEventBus eventBus;

    public AdminBlogPostCreateWidget(SimpleEventBus eventBus) {
        initWidget(uiBinder.createAndBindUi(this));
        this.eventBus = eventBus;

        submitButton.setText("Create");
        cancelButton.setText("Cancel");

    }

    @UiHandler("submitButton")
    void onSubmitButtonClick(ClickEvent event) {
        OnAdminBlogPostCreateResultEvent submitEvent = new OnAdminBlogPostCreateResultEvent();
        submitEvent.setResult(OnAdminBlogPostCreateResultEvent.CREATE);
        BlogPost resultBlogPost = new BlogPost();
        resultBlogPost.setCategory(categoryTextBox.getText());
        resultBlogPost.setDate(dateBox.getValue());
        resultBlogPost.setOwner(ownerTextBox.getText());
        resultBlogPost.setText(textArea.getText());
        resultBlogPost.setTheme(themeTextBox.getText());
        submitEvent.setResultBlogPost(resultBlogPost);
        eventBus.fireEvent(submitEvent);
    }

    @UiHandler("cancelButton")
    void onCancelButtonClick(ClickEvent event) {
        OnAdminBlogPostCreateResultEvent cancelEvent = new OnAdminBlogPostCreateResultEvent();
        cancelEvent.setResult(OnAdminBlogPostEditResultEvent.CANCEL);
        eventBus.fireEvent(cancelEvent);
    }
}
